//
//  MKOAuthViewController.m
//  Weibo_OC
//
//  Created by mac on 16/1/13.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MKOAuthViewController.h"

#import "Common.h"
#import "MKNetworkTools.h"
#import "MKUserAccount.h"
#import "MKUserModelView.h"
#import <SVProgressHUD.h>


@interface MKOAuthViewController () <UIWebViewDelegate>

@property (weak, nonatomic) UIWebView *webView;
@end

@implementation MKOAuthViewController

- (void)loadView {
    
    UIWebView *webView = [[UIWebView alloc] init];
    self.webView = webView;
    self.view = webView;
    webView.delegate = self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    [self loadWebViewRequest];

}
/**https://api.weibo.com/oauth2/authorize?client_id=2124965822&redirect_uri=www.itcast.cn 
 document.getElementById('passwd') 
 document.getElementById('passwd').focus()
 client_id	true	string	申请应用时分配的AppKey。
 redirect_uri	true	string	授权回调地址，站外应用需与设置的回调地址一致，站内应用需填写canvas page的地址。
 */
- (void)initView {
    self.webView.scrollView.bounces = NO;
    self.title = @"登录";
    
    [self setNav];
}

- (void)loadWebViewRequest {
    
    NSString *client_id = @"2124965822";
    NSString *redirect_uri = @"http://www.itcast.cn";
    
    NSString *urlString = [NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",client_id,redirect_uri];
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];
}

- (void)setNav {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"测试" style:UIBarButtonItemStylePlain target:self action:@selector(testBtnClick)];
}

- (void)testBtnClick {
    [self autoFillUserInfo];
}

- (void)autoFillUserInfo {
    NSString *js = @"document.getElementById('userId').value = 'fmake_one@126.com';document.getElementById('passwd').focus();document.getElementById('passwd').value = 'makemake'";
    [self.webView stringByEvaluatingJavaScriptFromString:js];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    
    NSString *urlString = request.URL.relativeString;
    NSString *codeFlag = @"code=";
    
    if ([urlString containsString:codeFlag]) {
        NSString *queryString = request.URL.query;
        NSString *code = [queryString substringFromIndex:codeFlag.length];

        [self loadTokenWithCode:code andSuccess:^(bool flag) {
            if (flag) {
                
                [SVProgressHUD dismiss];
                
                [self dismissViewControllerAnimated:YES completion:^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"setupRootViewController" object:@"welcome"];
                }];
                
            }
            else {
                
                [SVProgressHUD showErrorWithStatus:@"请检查网络连接..."];
            }
            
            
        }];
        
        return NO;
    }
    return  YES;
}

- (void)loadTokenWithCode:(NSString *)code andSuccess:(void (^)(bool flag))success {
    [[[MKUserModelView alloc] init] loadTokenWithCodeAtModelView:code andSuccess:success];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    
    [SVProgressHUD show];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [SVProgressHUD dismiss];
}









@end































